//
//  HomeHRVViewModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/10.
//

import UIKit
import MYModule

import RxCocoa
import RxSwift

public class HomeHRVViewModel: HomeBaseViewModel {
    
    public var date: Date?
    
    public let UIReplay: BehaviorRelay<HomeHRVModel>

    public required init(date: Date) {
        
        self.date = date
        
        let model = HomeHRVModel()
        
        UIReplay = BehaviorRelay<HomeHRVModel>(value: model)
        
        super.init(cellType: .hrv)
        
        reloadData(date)
        
        
        
    }
    
    // 查询单次测量HRV
    public func aSingleHRV() -> SingleHRVModel? {
        
        guard let hrvModel = SingleHRVModel.last(from: Date().timeIntervalSince1970) else {
            return nil
        }
        
        return hrvModel
        
    }
    
    public override func reloadData(_ date: Date? = nil) {
        
        
        reloadFullDayData(date: date)
        
        reloadMeasureState()
        
        reloadSingleData()
        
        showDateStr(date: date)
        
   
    }
    
    public func reloadFullDayData(date: Date?) {
        
        
        guard let date = date else {
            return
        }
        
        let model = UIReplay.value
        guard let hrvModel = HRVModel.day(date: date) else {
            
            let newModel = HomeHRVModel()
            newModel.measureState = model.measureState
            newModel.hasData = false
            UIReplay.accept(newModel)
            return
        }
        
        let dataModel = HRVChartDataModel(date: date, hrvModel: hrvModel)
        
        // 获取到图表数据
        model.chartMinData = dataModel.chartMinData
        model.chartMaxData = dataModel.chartMaxData
        model.chartDatas = dataModel.chartDatas
        model.chartYAxisCount = dataModel.chartYAxisCount
        
        model.hasData = dataModel.hasData
        
        UIReplay.accept(model)
        
    }
    
    public func showDateStr(date: Date?) {
        
        let model = UIReplay.value
        
        guard let date = date else {
            model.date = ""
            return
        }

        var fullDayDate: Date? = nil
        if let sqlModel = HeartRateModel.day(date: date) {
            fullDayDate = sqlModel.date
        }
        
        var singleDate: Date? = nil
        if let end = aSingleHRV()?.end {
            singleDate = Date(timeIntervalSince1970: end)
        }
        
        let lastDate = self.lastDate(fullDayDate: fullDayDate, singleDate: singleDate)
        if let lastDate = lastDate {
            model.date = Self.dateStr(date: lastDate)
        } else {
            model.date =  ""
        }
        
        self.date = lastDate
        
        UIReplay.accept(model)
    }
    
    
    public func reloadSingleData() {
        
        let model = UIReplay.value
        guard let singleHRVModel = aSingleHRV() else {
            model.hrv = 0
            UIReplay.accept(model)
            return
        }
        
        model.hrv = singleHRVModel.hrv
        
        UIReplay.accept(model)
    }
    
   
    @discardableResult
    public func reloadMeasureState() ->HomeHRVModel {
       
        
        let model = UIReplay.value
        
        if BluetoothManager.shared.deviceState == .connected {
            
            if BluetoothManager.shared.measuring == .HRV {
                model.measureState = .selected
            } else {
                model.measureState = (BluetoothManager.shared.measuring != .none || BluetoothManager.shared.isBatterycharging) ? .disabled : .normal
            }
            
        } else {
            model.measureState = .disabled
        }
        
        return model
    }
    
    
    public func automaticMeasure() {
        let model = UIReplay.value
        
        if model.measureState == .disabled {
            return
        }
        if model.measureState == .selected {
            measured()
        } else {
            measure()
        }
        
    }
    
    // 开始测量
    public func measure() {
        
        let model = UIReplay.value
        
        model.measureState = .selected
        
        BluetoothManager.shared.setStartHRV()
        
        print("已经在测量了")
        
       
        
    }
    
    // 结束测量
    public func measured() {
        
        if BluetoothManager.shared.measuring == .HRV {
            BluetoothManager.shared.setStopHRV()
        }
        
        measuredData()
        
        print("已经结束测量了")
        
    }
    
    public func measuredData() {
        
        let model = UIReplay.value
        model.measureState = .normal
        reloadData(self.date)
        
        
    }
    
}
